package core;

import java.awt.Point;
import java.util.Vector;

public class CustomUtil {

	
	
	public static Point pointOffset(Point inp, int offset){
		int oX=(int)(Math.random()*2*offset-offset);
		int oY=(int)(Math.random()*2*offset-offset);
		Point p=(Point)inp.clone();
		p.translate(oX, oY);
		return p;
	}
	public static void selectNextUnit(Player p){
		int unitIndex = p.getPlayerData().getUnits().indexOf(p.getSelectedUnit());
		int baseIndex = unitIndex;
		do{
			unitIndex++;
			if(p.getPlayerData().getUnits().size() <= unitIndex)
				unitIndex = 0;
			if(p.getPlayerData().getUnits().get(unitIndex).isAlive()){
				p.selectUnit( p.getPlayerData().getUnits().get(unitIndex) );
				break;
			}			
		}while (unitIndex != baseIndex);
	}
	/**
	 * 
	 * @param players
	 * @return winning player, null if there is no winner
	 */
	public static Player checkVictory(Vector<Player> players){
		Vector<Player> alivePlayers = new Vector<Player>();
		for(Player p : players){
			if(p.isAlive())
				alivePlayers.add(p);
		}
		if(alivePlayers.size() == 1)
			return alivePlayers.firstElement();
		
		return null;
	}
	
	
	
}
